/*当前页面刷新*/
$(".subfiled").on("click","h2",function(){
    refresh();
});

/*整体重定向*/
function refresh(){
    var len = arguments.length;
    /*则是当前页面刷新*/
    if (len === 0) window.location.reload();
    /*页面刷新,不带参*/
    if(len === 1){
        var page_name = arguments[0];//页面名
        window.location.href = page_name;
    }
    /*页面刷新,带参*/
    if(len === 2) {
        var page_name = arguments[0];   //页面名
        var param = arguments[1];       //携带参数
        if (!(param instanceof Object))
            param = eval('(' + param + ')');
        var param_url = "?";
        for(var p in param) param_url += p +"=" + param[p] + "&";
        window.location.href = page_name + param_url;
    }
}

/*详情*/
$("td").on("click","button[city-detail]",function(){
    var that = $(this);
    var detailId = that.attr("detail-id");
    var detailSrc = that.attr("city-detail");
    window.location.href = detailSrc + "?id=" + detailId;
});

/*事件*/
$("body").on("click","button[city-event]",function(){
    var that = $(this);
    var event = that.attr("city-event");
    console.log(event);
    switch (event){
        case "doban-control": doban_control(that); break;
        case "doban-cancel": doban_cancel(that); break;
        case "report-del": report_del(that);break;
        // case "report-accept": report_accept(that);break;
        case "report-false": report_false(that);break;
        case "report-self-do": report_self_do(that);break;
        case "report-dispatch": report_dispatch(that);break;
        case "branch-check": branch_check(that);break;
        case "business-complete": business_complete(that);break;
        case "business-dispatch": business_dispatch(that);break;
        case "business-back": business_back(that);break;
        case "business-record": business_record(that);break;
        case "add-time": add_time(that);break;
        case "business-do-ban": business_do_ban(that);break;
        case "business-together": business_together(that);break;
        case "business-auth": business_auth(that);break;
        case "business-print": business_print(that);break;
        case "business-back-task": business_back_task(that);break;
        case "checks-accept": checks_accept(that);break;
        case "checks-false": checks_false(that);break;
        case "report-back": report_back(that);break;
        case "pass-check": pass_check(that);break;
        case "pass-failed": pass_failed(that);break;
        case "message-back": message_back(that);break;
        default:break;

    }
});

/*督办*/
function doban_control(obj){
    var report_id = obj.attr("event-id");
    $.post("/ajax/colligate/dobanControl",{
        id:report_id
    },function(res){
        if(res.code === 1)
            layer.msg("督办成功！<span class='color-yellow'>"+res.data.title+"</span> &nbsp;案件处置的所有改变都会推送消息给您！",{},function(){refresh()});
        else
            layer.msg("操作失败！");
    });
}

/*督办取消*/
function doban_cancel(obj){
    var report_id = obj.attr("event-id");
    $.post("/ajax/colligate/dobanCancel",{
        id:report_id
    },function(res){
        if(res.code === 1)
            layer.msg("取消督办成功！<span class='color-yellow'>"+res.data.title+"</span> &nbsp;案件处置的相关通知都不会发送给您！",{},function(){refresh()});
        else
            layer.msg("操作失败！");
    });
}

/*上报案件删除*/
function report_del(obj){
    layer.confirm("您确认要这样操作吗？",{},function() {
        var report_id = obj.attr("event-id");
        $.post("/ajax/colligate/reportDel", {
            id: report_id
        }, function (res) {
            if(res.code === 1){
                obj.parents("tr").hide();
                layer.msg("操作成功！");
            }else
                layer.msg("操作失败！");
        });
    });
}



/*不同意上报*/
function report_false(obj){
    layer.confirm("您确认要这样操作吗？",{},function(){
        var report_id = obj.attr("event-id");
        $.post("/ajax/colligate/reportScheduleChange",{
            id:report_id,
            schedule:-1
        },function(res){
            layer.msg(res.code === 1 ? "操作成功！" : "操作失败！",{},function(){
                refresh();
            });
        });
    });
}

/*自行处置案件*/
function report_self_do(obj){
    layer.confirm("您确认要这样操作吗？",{},function(){
        var report_id = obj.attr("event-id");
        $.post("/ajax/colligate/selfDo",{
            id:report_id
        },function(res){
            layer.msg(res.code === 1 ? "操作成功！" : "操作失败！",{},function(){
                refresh();
            });
        });
    });
}

/*派遣专业部门*/
function report_dispatch(obj){

}



/*指挥派遣*/
function business_dispatch(obj){

    //拿到部门的相应人员
    // layer.confirm("您确认要这样操作吗？",{},function(){
    //     var report_id = obj.attr("event-id");
    //     $.post("/ajax/colligate/reportScheduleChange",{
    //         id:report_id,
    //         schedule:3
    //     },function(res){
    //         layer.msg(res.code === 1 ? "指挥派遣成功！请尽快处理！" : "操作失败！",{},function(){
    //             refresh();
    //         });
    //     });
    // });

    layer.confirm("您确认要这样操作吗？",{},function(){
        var report_id = obj.attr("event-id");
        $.post("/ajax/business/findBranchPerson",{

        },function(res){
            if(res.code===1){
                var str = '<div id="detail-branch"><div class="buttons" report-id="'+report_id+'">';
                var peopleList = res.data;
                for(var i = 0;i < peopleList.length;i++)
                    str += '<button city-event="branch-check" people-id="'+peopleList[i]['id']+'" class="layui-btn layui-btn-sm">'+peopleList[i]['name']+'</button>';
                str += '</div></div>';
                layer.open({
                    type: 1,
                    title: "选择派遣人员",
                    closeBtn: 1,
                    shadeClose: true,
                    content: str,
                    fixed:false,
                    offset: '100px',
                });
                layer.msg("请选择派遣的人员！");
            }else
                layer.msg("读取人员数据失败！");

        });
    });
}

/*选择派遣人员*/
function branch_check(obj){
    var report_id = obj.parent().attr("report-id");
    var people_id = obj.attr("people-id");
    var people_name = obj.html();
    layer.confirm("您确认要派遣给<span class='color-yellow'>"+people_name+"</span>吗？",{},function(){
        layer.msg("正在派遣中...");
        $.post("/ajax/colligate/dispatchPeople",{
            reportId:report_id,
            peopleId:people_id
        },function(res) {
            if (res.code === 1) {
                layer.alert("派遣成功！",{},function(){refresh()});
            }
        });
    })
}

/*处置完成*/
function business_complete(obj){
    layer.confirm("您确认处置已经完成了吗？",{},function(){
        var report_id = obj.attr("event-id");
        var nowTime = obj.parent().parent().children().eq(0).html();
        var str = '<div class="kv-item clearfix">\n' +
            '                        <label class="layui-btn layui-btn-xs" id="files">选择附件</label>\n' +
            '                        <div class="kv-item-content" style="width: 600px;">\n' +
            '                            <div class="layui-upload-list">\n' +
            '                                <table class="layui-table">\n' +
            '                                    <thead>\n' +
            '                                    <tr>\n' +
            '                                        <th>文件名</th>\n' +
            '                                        <th>大小</th>\n' +
            '                                        <th>状态</th>\n' +
            '                                        <th>操作</th>\n' +
            '                                    </tr>\n' +
            '                                    </thead>\n' +
            '                                    <tbody id="fileList"></tbody>\n' +
            '                                </table>\n' +
            '                            </div>\n' +
            '                        </div>\n' +
            '                    </div>'+'<div style="text-align: center">\n' +
            '                <button id="submitInfo" class="layui-btn layui-btn-danger layui-btn-xs"\n' +
            '                       >确定\n' +
            '                </button>\n' +
            '            </div>';
        layer.open({
            type: 1,
            title: "上传现场图片",
            area: ['700px','70%'],
            closeBtn: 1,
            shadeClose: true,
            content: str,
            fixed:false,
            offset: '100px',
        });

        var fileList = {}, demoListView = $('#fileList')
            , uploadListIns = upload.render({
            elem: '#files'
            ,url:'/api/spfile/upload'  //上传接口
            ,data:{
                tableName: '任务表',
                index:nowTime
            }
            , accept: 'file'
            , multiple: true
            , size: 200*1024
            , auto: true
            ,bindAction: '#files'
            , choose: function (obj) {
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                //读取本地文件
                obj.preview(function (index, file, result) {
                    var tr = $(['<tr id="upload-' + index + '">'
                        , '<td>' + file.name + '</td>'
                        , '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>'
                        , '<td>等待上传</td>'
                        , '<td>'
                        , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                        , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                        , '</td>'
                        , '</tr>'].join(''));

                    //单个重传
                    tr.find('.demo-reload').on('click', function () {
                        obj.upload(index, file);
                    });

                    //删除
                    tr.find('.demo-delete').on('click', function () {
                        delete files[index]; //删除对应的文件
                        delete fileList[index];
                        tr.remove();
                        uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });

                    demoListView.append(tr);
                });
            }
            , done: function (res, index, upload) {
                if (res.code === 0) { //上传成功
                    var tr = demoListView.find('tr#upload-' + index)
                        , tds = tr.children();
                    tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                    fileList[index] = res.data.src;
                    return delete this.files[index]; //删除文件队列已经上传成功的文件
                }
                this.error(index, upload);
            }
            , error: function (index, upload) {
                var tr = demoListView.find('tr#upload-' + index)
                    , tds = tr.children();
                tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
            }
        });
        $("#submitInfo").click(function () {
            if($("#fileList:has(tr)").length>0){
                $.post("/ajax/colligate/reportFinish",{
                    id:report_id,
                    schedule:4
                },function(res){
                    layer.msg(res.code === 1 ? "操作成功！请尽快反馈给指挥中心！" : "操作失败！",{},function(){
                        refresh();
                    });
                });
            }else {
                layer.alert("请上传对比图片!");
            }

        })

    });
}

/*处置反馈*/
function business_back(obj){
    var report_id = obj.attr("event-id");
    var taskId = obj.attr("event-task");
    layer.open({
        type: 1,
        area: ['70%','590px'],
        title: '反馈编号：' + taskId,
        content: open_business_back(report_id)
    });
    /*初始化文本编辑器*/
    {
        //配置图片上传接口
        layedit.set({
        uploadImage: {
            url: 'http://192.168.127.134:2019/upload' //接口url
            , type: 'post' //默认post
        }
    });
    var editIndex = layedit.build('businessBack', {
        height: 400,
        tool: ['strong', 'italic', 'underline', 'del', '|', 'left', 'center', 'right', 'link', 'unlink', 'face', 'image']
    });
    $("html").on("click","button[city-event=business_back_sure]",function(){

        layer.confirm("您确认要这样操作吗？",{},function() {
            var content = layedit.getContent(editIndex);
            //提交信息到指挥中心
            $.post("/ajax/business/feedback",{
                    id:report_id,
                    content:content
                },function(res){
                    if(res.code !== 1){
                        layer.msg("请求错误！");
                    }else{
                        layer.msg("反馈成功！",{},function(){refresh()});
                    }
                }
            );
        });
    });
}
}

/*处置督办*/
function business_do_ban(obj){
    var report_id = obj.attr("event-id");
    layer.open({
        type: 1,
        area: ['486px'],
        title: '督办编号：' + report_id,
        content: do_ban_comment()
    });
    $("html").on("click","button[city-event=do_ban_comment_sure]",function(){
        layer.confirm("您确认要这样操作吗？",{},function() {
            var comment = $("#doBanComment").val();

            //提交信息到指挥中心
            $.post("/ajax/business/do_ban",{
                reportId:report_id,
                comment:comment
            },function(res){
                layer.msg(res.code === 1 ? "操作成功！已经督促指定部门尽快处置！" : "操作失败！",{},function(){
                    refresh();
                });
            });
        });
    });
}

/*申请会办*/
function business_together(obj){
    var report_id = obj.attr("event-id");
    var task_id = obj.attr("event-task");
    layer.open({
        type: 1,
        area: ['486px'],
        title: '申请编号：' + task_id,
        content: together()
    });
    $("html").on("click","button[city-event=together_sure]",function(){
        layer.confirm("您确认要这样操作吗？",{},function() {
            var content = $("#togetherContent").val();
            if(content == null || content === ""){
                layer.msg("申请理由不能为空哦！")
            } else
                $.post("/ajax/business/apply_together",{
                    reportId:report_id,
                    content:content
                },function(res){
                    layer.msg(res.code === 1 ? "操作成功！已经向指挥中心发出申请！" : "操作失败！",{},function(){
                        refresh();
                    });
                });
        });
    });
}

/*申请授权*/
function business_auth(obj){
    var report_id = obj.attr("event-id");
    var task_id = obj.attr("event-task");

    layer.open({
        type: 1,
        area: ['350px','100px'],
        title: '申请编号：' + task_id,
        offset: '100px',
        content: '<button style="margin: 10px 90px 15px 50px" class="layui-btn layui-btn-sm" city-event="add-time"\n' +
        '                                            event-id='+ report_id +' event-task='+ task_id +'>申请延期\n' +
        '                                    </button>\n' +
        '                                    <button class="layui-btn layui-btn-danger layui-btn-sm" city-event="business-back-task"\n' +
        '                                            event-id='+ report_id +'  event-task='+ task_id +'>申请撤回\n' +
        '                                    </button>'
    });
    // $("html").on("click","button[city-event=business_auth_sure]",function(){
    //     layer.confirm("您确认要这样操作吗？",{},function() {
    //         var content = $("#doBanComment").val();
    //         if(content == null || content === ""){layer.msg("申请理由不能为空哦！")}
    //         else
    //             $.post("/ajax/business/apply_auth",{
    //                 reportId:report_id,
    //                 content:content
    //             },function(res){
    //                 layer.msg(res.code === 1 ? "操作成功！已经向指挥中心发出申请！" : "操作失败！",{},function(){
    //                     refresh();
    //                 });
    //             });
    //     });
    // });
}

/*业务信息打印*/
function business_print(obj){
    window.print();
}

/*督促记录*/
function business_record(obj){
    var report_id = obj.attr("event-id");
    $.post("/ajax/business/recordList",{
        reportId : report_id
    },function(res){
        if(res.code === 1){
            layer.open({
                type: 1,
                shadeClose: true,
                area : ['600px'],
                title: '督办编号：' + report_id,
                content: do_ban_record(res.data)
            });
        }else
            layer.msg("操作失败！");
    });

}

/*核查结案*/
function checks_accept(obj){
    var report_id = obj.attr("event-id");
    layer.confirm("您确认要这样操作吗？",{},function() {
        $.post("/ajax/colligate/checkAccept",{
            id : report_id
        },function (res) {
            if(res.code === 1){
                layer.msg(res.code === 1 ? "操作成功！" : "操作失败！",{},function(){
                    refresh();
                });
            }
        });
    });
}

/*拒绝结案*/
function checks_false(obj){
    layer.msg("不能结案哦，还没做完");
}

/*案卷回撤*/
function report_back(obj){
    var report_id = obj.attr("event-id");
    layer.open({
        type: 1,
        area: ['486px'],
        title: '回撤案件编号：' + report_id,
        content: report_back_edit()
    });
    $("html").on("click","button[city-event=report_back_edit_sure]",function(){
        layer.confirm("您确认要这样操作吗？",{},function() {
            var remark = $("#reportBackEdit").val();
            $.post("/ajax/colligate/reportBack",{
                id: report_id,
                remark: remark
            },function (res) {
                if(res.code === 1){
                    layer.msg(res.code === 1 ? "操作成功！" : "操作失败！",{},function(){
                        refresh();
                    });
                }
            });
        });
    });
}

/*上报表单验证*/
function report_valid(obj) {

    if (obj.villageId === null
        || obj.townId === null
        || obj.typeBig === ""
        || obj.gis === ""
        || obj.title === ""
        || obj.address === ""
        || obj.content === "") {
        layer.msg("请先完善表单信息！");
        return false;
    }

    obj.typeBig = parseInt(obj.typeBig);
    obj.typeSmall = parseInt(obj.typeSmall);
    return true;
}

/*审核通过*/
function pass_check(obj) {
    var report_id = obj.attr("event-id");
    layer.confirm("您确认要这样操作吗？",{},function() {
        $.post("/ajax/colligate/checkPass",{
            id : report_id
        },function (res) {
            if(res.code === 1){
                layer.msg(res.code === 1 ? "操作成功！" : "操作失败！",{},function(){
                    refresh();
                });
            }
        });
    });
}

/*
返工
 */
function pass_failed(obj) {

    var report_id = obj.attr("event-id");
    var task_id = obj.attr("event-task");
    layer.open({
        type: 1,
        area: ['486px'],
        title: '返工编号：' + task_id,
        content: do_ban_comment()
    });
    $("html").on("click","button[city-event=do_ban_comment_sure]",function(){
        layer.confirm("您确认要这样操作吗？",{},function() {
            var comment = $("#doBanComment").val();

            //提交信息到指挥中心
            $.post("/ajax/colligate/passFailed",{
                id : report_id,
                taskId:task_id,
                comment:comment
            },function (res) {
                if(res.code === 1){
                    layer.msg(res.code === 1 ? "操作成功！" : "操作失败！",{},function(){
                        refresh();
                    });
                }
            });
        });
    });
}

/**
 * 任务撤回
 */

function business_back_task(obj) {
    var report_id = obj.attr("event-id");
    var task_id = obj.attr("event-task");
    layer.open({
        type: 1,
        area: ['486px'],
        title: '撤回编号：' + task_id,
        content: do_ban_comment()
    });
    $("html").on("click","button[city-event=do_ban_comment_sure]",function(){
        layer.confirm("您确认要这样操作吗？",{},function() {
            var comment = $("#doBanComment").val();

            //提交信息到指挥中心
            $.post("/ajax/colligate/caseback",{
                id : report_id,
                taskId:task_id,
                comment:comment
            },function (res) {
                if(res.code === 1){
                    layer.msg(res.code === 1 ? "操作成功！" : "操作失败！",{},function(){
                        refresh();
                    });
                }
            });
        });
    });
}

function add_time(obj) {

    var report_id = obj.attr("event-id");
    var task_id = obj.attr("event-task");

    layer.open({
        type: 1,
        area: ['486px'],
        title: '延期编号：' + task_id,
        content: business_auth_alert()
    });

    $("html").on("click","button[city-event=business_auth_sure]",function(){
        layer.confirm("您确认要这样操作吗？",{},function() {
            var comment = $("#doBanComment").val();


            //提交信息到指挥中心
            $.post("/ajax/business/apply_auth",{
                reportId : report_id,
                content:comment
            },function (res) {
                if(res.code === 1){
                    layer.msg(res.code === 1 ? "操作成功！" : "操作失败！",{},function(){
                        refresh();
                    });
                }
            });
        });
    });

}

/*消息反馈*/
function message_back(obj){
    var id = obj.attr("event-id");
    var msg = obj.attr("event-msg");
    var taskId = obj.attr("event-task");
    layer.confirm("您确认要这样操作吗？",{},function() {
        $.post("/ajax/home/msgBack",{
            id : id,
            msg : msg,
            taskId:taskId
        },function (res) {
            if(res.code === 1){
                layer.msg(res.code === 1 ? "操作成功！" : "操作失败！",{},function(){
                    refresh();
                });
            }
        });
    });
}

